<div class="amp-content">
  <h3 class="amp-header"><?php echo $action_title; ?></h3>

  <form action="" method="post" enctype="multipart/form-data" class="amp-container">
    <?php while ($obj = $data->next()) { ?>
    <div class="amp-content">
      <?php
      $elements->reset();

      while ($element = $elements->next()) {
        echo $element->render($obj, $action);
      }
      ?>

      <div style="clear:both;"></div>

      <div id="tabs" style="display: none;"><ul></ul></div>

      <?php if ($obj->$pk) { ?>
      <nav class="toolbar-container">
        <ul class="toolbar">
          <li class="toolbar-item">
            <?php echo $this->menu_item->show(array('label' => 'Delete', 'icon' => 'delete', 'route' => $route,
              'params' => array('action' => 'delete', $pk => $obj->$pk), 'linkParams' => array('onclick' => "return confirm('Do you confirm this action?');"))) ?>
          </li>
        </ul>
        <div style="clear:both;"></div>
      </nav>
      <?php } ?>
    </div>
    <?php } ?>

    <nav class="toolbar-container">
      <ul class="toolbar">
        <li class="toolbar-item"><input type="submit" value="Save"/></li>
      </ul>

      <div style="clear:both;"></div>
    </nav>
  </form>
</div>

<style>
.form_element_text {
  width:400px;
}

.form_element_textarea {
  width:600px;
  height:200px;
}
</style>

<script>
(function($){
  $.fn.extend({
    formSubmit: function(options) {
      var defaults = {};

      var options = $.extend(defaults, options);

      return this.each(function() {
        var obj = $(this);

        obj.submit(function() {
          $(this).find(':input').attr('disabled', 'disabled');

          $.post(options.url, $(this).find(':input'), function(response) {
            obj.parents('.amp-content').parent().html(response);
          });

          return false;
        });
      });
    }
  });
})(jQuery);

$(document).ready(function() {
  <?php if ($request->ajax()) { ?>
    <?php $data->reset(); while ($obj = $data->next()) { ?>
      $('form.amp-container').formSubmit({ url:'<?php echo $response->makeUrl($route, array('action' => $action_name, $pk => $obj->$pk)) ?>' });
    <?php } ?>
  <?php } ?>

  if ($('.form-fieldset').length) {
    $('.form-fieldset').each(function(index, item) {
      var label = $(item).find('legend').html();
      var content = $(item).find('.form-element-body').html();

      var li = '<li><a href="#tab-'+(index + 1)+'">'+label+'</a></li>';
      var div = '<div id="tab-'+(index + 1)+'">'+content+'</div>';

      $('#tabs ul').append(li);
      $('#tabs').append(div);
    }).remove();

    $('#tabs').tabs().show();
  }
});
</script>